//关注机构
exports.concernOrganization = function(req,res) {

    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;

    var is_concern = 0;

    var organizationConcernData = 0;

    var resultData = {};
    //任务流创建
    async.waterfall([

        //查询是否已经关注过该机构
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganizationConcern + "/uoc_oid/" + bodyParams.o_id + "/uoc_uid/" + bodyParams.u_id,
                    method: 'POST',
                    data: {
                        fields: ["uoc_id", "is_delete"].join(",")
                    }
                },
                function (err, data) {
                    //console.log(data);
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    is_concern = data.data;
                    cb();
                }
            );
        },
        function (cb) {
            //已关注
            if (is_concern.length !== 0) {
                if (is_concern[0].is_delete <= 0) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganizationConcern,
                            method: 'POST',
                            data: {
                                uoc_id: is_concern[0].uoc_id,
                                is_delete: 1
                            }
                        },
                        function (err, data) {
                            // //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }
                            resultData.cancleConcern = "已取消关注";
                            cb();
                        }
                    );
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganizationConcern,
                            method: 'POST',
                            data: {
                                uoc_id: is_concern[0].uoc_id,
                                is_delete: 0
                            }
                        },
                        function (err, data) {
                            // //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }
                            resultData.reConcern = "关注成功";
                            cb();
                        }
                    );
                }
            } else {
                //未关注
                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.addOrganizationConcern,
                        method : 'POST',
                        data   : {
                            uoc_oid : bodyParams.o_id,
                            uoc_uid : bodyParams.u_id
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res,false,"机构关注失败，请重新尝试");
                        }
                        resultData.concern = "关注成功";
                        cb();
                    }
                );
            }
        },
        //查询关注数
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + bodyParams.o_id,
                    method: 'POST',
                    data: {
                        fields: ["o_id", "o_concern_count"].join(",")
                    }
                },
                function (err, data) {
                    //console.log(data);
                    //判断是否有错误信息
                    if (err) {
                        return RES.response(res, false, data);
                    }

                    organizationConcernData = data.data;
                    // //console.log("------------------11");
                    cb();
                }
            );

        },
        //3.关注数更新
        function (cb) {
            if (is_concern.length !== 0) {
                if (is_concern[0].is_delete <= 0) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganization,
                            method: 'POST',
                            data: {
                                o_id: bodyParams.o_id,
                                o_concern_count: parseInt(organizationConcernData[0].o_concern_count) - 1
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            resultData.updateData = data;
                            cb();
                        }
                    );
                }else{
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganization,
                            method: 'POST',
                            data: {
                                o_id: bodyParams.o_id,
                                o_concern_count: parseInt(organizationConcernData[0].o_concern_count) + 1
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            resultData.updateData = data;
                            cb();
                        }
                    );
                }
            }else{
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.updateOrganization,
                        method: 'POST',
                        data: {
                            o_id: bodyParams.o_id,
                            o_concern_count: parseInt(organizationConcernData[0].o_concern_count) + 1
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }

                        resultData.updateData = data;
                        cb();
                    }
                );
            }
        }
        ],function(err, result){

            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );

}